<template>
    <div class="Login">
        <div class="wrapper">
            <YsMLogin
                ref="myLogin"
                :btnLoading="btnLoading"
                :loginStatus="loginStatus"
                :borderDisplay="borderDisplay"
                :isShowModifyPwd="isShowModifyPwd"
                @on-handle-login="handleLogin"
                @on-apply="onApply"
                @on-resetPwd="handleResetPwd"
                @on-handle-modify="handleModifyPwd"
            ></YsMLogin>
        </div>
    </div>
</template>

<script>
import { ys_SysAction } from 'ys-utils-js';
export default {
    name: 'YsLoginTest',
    components: {},
    data() {
        return {
            // 传入是否显示border
            borderDisplay: true,
            // loading状态
            btnLoading: false,
            // 申请状态
            loginStatus: 'adopt',
            // 存储当前登录用户名
            loginUserName: {},
            // 是否显示修改密码页面
            isShowModifyPwd: false,
        };
    },
    methods: {
        // 登录回调
        handleLogin(res) {
            this.btnLoading = true;
            this.loginUserName.org_user_id = res.org_user_id;
            this.login(res);
        },
        // 登录
        async login(res) {
            try {
                const result = await ys_SysAction.userLogin(res, window.GlobalConfig.PaaSCode);
                // 判断接口返回的状态码 -1 需要修改密码 -2 需要申请白名单 -3 申请白名单还在审核中
                switch (result) {
                    case -1:
                        this.$YsMessage.info('新用户请重置密码！');
                        this.isShowModifyPwd = true;
                        break;
                    case -2:
                        this.loginStatus = 'apply';
                        break;
                    case -3:
                        this.loginStatus = 'underReview';
                        break;
                    case true:
                        this.$YsMessage.success('登录成功');
                        this.$router.push({
                            name: 'home',
                        });
                        break;
                    default:
                        this.$YsMessage.error('登录失败');
                        // 清空表单
                        this.$refs.myLogin.clearFrom();
                        break;
                }
                this.btnLoading = false;
            } catch (error) {
                this.$YsMessage.error('登录失败');
                this.$refs.myLogin.clearFrom();
                this.btnLoading = false;
            }
        },
        // 是否申请白名单
        async onApply(state) {
            this.loginUserName.org_rs_code = window.GlobalConfig.PaaSCode;
            if (state) {
                const result = await ys_SysAction.addUserTermAction(this.loginUserName);
            } else {
                this.$refs.myLogin.clearFrom();
            }
        },
        // 申请重置密码
        async handleResetPwd(userInfo) {
            const result = await ys_SysAction.UserApplyResetPwd(userInfo);
            if (result.status === 'ok') {
                this.$YsMessage.success('申请重置密码提交成功！');
            } else {
                this.$YsMessage.error(result.msg);
            }
        },
        // 修改密码
        async handleModifyPwd(modifyInfo) {
            const result = await ys_SysAction.resetPwd(modifyInfo);
            if (result.status === 'ok') {
                this.$YsMessage.success('修改密码成功，请登录！');
                setTimeout(() => {
                    this.isShowModifyPwd = false;
                }, 1000);
            } else {
                this.$YsMessage.error(result.msg);
            }
        },
    },
};
</script>
<style lang="less" scoped>
.Login {
    width: 100%;
    height: 100vh;
    background: #273b60;
    display: flex;
    justify-content: center;
    align-items: center;

    .wrapper {
        width: 400px;
    }
}
</style>
